import { ref } from 'vue'
import { defineStore } from 'pinia'
import http_login from '@/api/login'

export const useUserStore = defineStore(
  'user',
  () => {
    /* 
        定义用户的token，setToken()，removeToken()
    */
    const token = ref('')
    const setToken = (newToken) => {
      token.value = newToken
    }
    const removeToken = () => {
      token.value = ''
    }
    // 定义用户的信息 userInfo, getUserInfo()
    const userInfo = ref({})
    const getUserInfo = async () => {
      const result = await http_login.getUserInfo()
      if (result.code === 200) {
        userInfo.value = result.data
      }
    }
    const setUserInfo = (data) => {
      userInfo.value = data
    }
    return { token, userInfo, setToken, removeToken, getUserInfo, setUserInfo }
  },
  {
    persist: {
      paths: ['token', 'userInfo']
    }
  }
)
